<?


class objProcessor {
	public $id = 0;
	public $index = 0;
	public $type = '';
	public $description = '';
	public $load = 0;
	public function __construct($cfg=array()) {
		if (is_array($cfg) || is_object($cfg)) {
			foreach ($cfg AS $name=>$value) {
				switch ($name) {
					case 'hrDeviceIndex':
						$this->index = $value;
						break;
					case 'hrDeviceType':
						$this->type = $value;
						break;
					case 'hrDeviceDescr':
						$this->description = $value;
						break;
					case 'hrProcessorLoad':
						$this->load = (int)$value;
						break;
				
				}
			
			
			}		
		}	
	}
	
	
	public function getDescription() {
	
		if (preg_match("/^GenuineIntel/", $this->description)) {
			list($description,$speed) = explode('@',substr($this->description,strlen('GenuineIntel:')));
			return str_replace('(R)','&reg;',trim($description));
		} elseif (preg_match("/^Unknown Processor Type/", $this->description)) {
			return 'Intel Xeon Processor';
		} else {
			return $this->description;	
		}
	}
	public function getSpeed() {	
		if (preg_match("/^GenuineIntel/", $this->description)) {
			list($description,$speed) = explode('@',substr($this->description,strlen('GenuineIntel:')));
			return trim($speed);
		} else {
			return '';	
		}
	}
	
	public function getPercentageLoad() { return $this->load; }
	public function storeUsageRrd($path,$usage=0,$step=60) {
		if (!is_file($path)) {
			$opts = array(
				'--step',$step,
				'DS:usage:GAUGE:600:-273:1000',
				'RRA:MIN:0.5:12:2400',
				'RRA:MAX:0.5:12:2400',
				'RRA:AVERAGE:0.5:12:2400'
			);
			if (rrd_create($path, $opts, count($opts)) == 0) { 
				echo 'Error: '.rrd_error().chr(10);
				// Log Error
				return false;
			}
		}		
		$rrd = array();
		$rrd[] = 'N';
		$rrd[] = $usage==0?$this->load:$usage;
		echo "Processor (".$this->index."): ".implode(':',$rrd).chr(10);
		if (rrd_update($path,implode(':',$rrd)) == 0) {
			echo rrd_error();
			return false;
		} else {
			return true;
		}
	}
 }
?>